package com.keqing.tomato.fragment;

import static android.content.Context.MODE_PRIVATE;
import static com.keqing.tomato.bean.MySetting.default_focus_sound;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.keqing.tomato.AbountMeActivity;
import com.keqing.tomato.RadioListActivity;
import com.keqing.tomato.R;
import com.keqing.tomato.UseManualActivity;
import com.keqing.tomato.bean.MySetting;
import com.keqing.tomato.bean.Sound;


public class FragmentSettings extends Fragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private View focus_time_container;
    private TextView tv_focus_time;
    private View rest_time_container;
    private TextView tv_rest_time;
    private View tomato_finish_ring_container;
    private TextView tv_tomato_finish_ring;
    private View rest_finish_ring_container;
    private TextView tv_tomato_rest_ring;
    private Switch switch_vibration;
    private Switch switch_auto_rest;
    private TextView use_manual;
    private TextView abount_me;
    private Activity activity;

    private int focusTime;
    private int restTime;
    private String focusSound;//filepath
    private String restSound;//filepath
    private boolean vibration;
    private boolean autoRest;
    public static int request_code_time_focus = 0;
    public static int request_code_time_rest = 1;
    public static int request_code_sound_focus = 2;
    public static int request_code_sound_rest = 3;
    private SharedPreferences.Editor editor;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e("tomato", "FragmentSettings - onCreateView()");
        View view = inflater.inflate(R.layout.fragment_settings, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.e("tomato", "FragmentSettings - onViewCreated()");
        activity = getActivity();
        editor = activity.getSharedPreferences("mysettings", MODE_PRIVATE).edit();


        tv_rest_time = view.findViewById(R.id.tv_rest_time);
        tv_focus_time = view.findViewById(R.id.tv_focus_time);
        rest_time_container = view.findViewById(R.id.rest_time_container);
        tomato_finish_ring_container = view.findViewById(R.id.tomato_finish_ring_container);
        rest_finish_ring_container = view.findViewById(R.id.rest_finish_ring_container);
        tv_tomato_finish_ring = view.findViewById(R.id.tv_tomato_finish_ring);
        tv_tomato_rest_ring = view.findViewById(R.id.tv_tomato_rest_ring);
        use_manual = view.findViewById(R.id.use_manual);
        abount_me = view.findViewById(R.id.abount_me);
        switch_vibration = view.findViewById(R.id.switch_vibration);
        switch_auto_rest = view.findViewById(R.id.switch_auto_rest);
        focus_time_container = view.findViewById(R.id.focus_time_container);

        focus_time_container.setOnClickListener(this);
        rest_time_container.setOnClickListener(this);
        tomato_finish_ring_container.setOnClickListener(this);
        rest_finish_ring_container.setOnClickListener(this);
        use_manual.setOnClickListener(this);
        abount_me.setOnClickListener(this);
        switch_auto_rest.setOnCheckedChangeListener(this);
        switch_vibration.setOnCheckedChangeListener(this);

        initSettings();

    }

    private void initSettings() {
        SharedPreferences preferences = activity.getSharedPreferences("mysettings", MODE_PRIVATE);
        focusTime = preferences.getInt("focusTime", MySetting.default_focus_time);
        restTime = preferences.getInt("restTime", MySetting.default_rest_time);
        String focusSoundPath = preferences.getString("focusSound", default_focus_sound);
        String restSoundPath = preferences.getString("restSound", MySetting.default_rest_sound);
        for (int i = 0; i < MySetting.soudList.size(); i++) {
            Sound soundBean = MySetting.soudList.get(i);
            if (soundBean.getFilePath().equals(focusSoundPath)) {
                focusSound = soundBean.getCnName();
            }
            if(soundBean.getFilePath().equals(restSoundPath)){
                restSound = soundBean.getCnName();
            }
        }
        vibration = preferences.getBoolean("vibration", MySetting.default_vibration);
        autoRest = preferences.getBoolean("autoRest", MySetting.default_auto_rest);

        tv_focus_time.setText(focusTime + "分钟");
        tv_rest_time.setText(restTime + "分钟");
        tv_tomato_finish_ring.setText(focusSound);
        tv_tomato_rest_ring.setText(restSound);
        switch_vibration.setChecked(vibration);
        switch_auto_rest.setChecked(autoRest);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("tomato", "FragmentSettings - onDestroy()");
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.focus_time_container) {
            Intent intent = new Intent(activity, RadioListActivity.class);
            intent.putExtra("requestCode", request_code_time_focus);
            intent.putExtra("title", "番茄钟时长");
            intent.putExtra("data", MySetting.tomato_time_array);
            intent.putExtra("time", focusTime);
            startActivityForResult(intent, request_code_time_focus);
        }
        if (v.getId() == R.id.rest_time_container) {
            Intent intent = new Intent(activity, RadioListActivity.class);
            intent.putExtra("requestCode", request_code_time_rest);
            intent.putExtra("title", "休息时长");
            intent.putExtra("data", MySetting.rest_time_array);
            intent.putExtra("time", restTime);
            startActivityForResult(intent, request_code_time_rest);
        }
        if (v.getId() == R.id.tomato_finish_ring_container) {
            Intent intent = new Intent(activity, RadioListActivity.class);
            intent.putExtra("requestCode", request_code_sound_focus);
            intent.putExtra("title", "番茄钟完成铃声");
            intent.putExtra("sound", focusSound);
            startActivityForResult(intent, request_code_sound_focus);
        }
        if (v.getId() == R.id.rest_finish_ring_container) {
            Intent intent = new Intent(activity, RadioListActivity.class);
            intent.putExtra("requestCode", request_code_sound_rest);
            intent.putExtra("title", "休息完成铃声");
            intent.putExtra("sound", restSound);
            startActivityForResult(intent, request_code_sound_rest);
        }
        if (v.getId() == R.id.use_manual) {
            Intent intent = new Intent(activity, UseManualActivity.class);
            startActivity(intent);
        }else if (v.getId() == R.id.abount_me) {
            Intent intent = new Intent(activity, AbountMeActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data == null) return;
        if(requestCode == request_code_time_focus){
            focusTime = data.getIntExtra("time", MySetting.default_focus_time);
            tv_focus_time.setText(this.focusTime + "分钟");
        }
        if(requestCode == request_code_time_rest){
            restTime = data.getIntExtra("time", MySetting.default_rest_time);
            tv_rest_time.setText(this.restTime + "分钟");
        }
        if(requestCode == request_code_sound_focus){
            focusSound = data.getStringExtra("sound");
            tv_tomato_finish_ring.setText(focusSound);
        }
        if(requestCode == request_code_sound_rest){
            restSound = data.getStringExtra("sound");
            tv_tomato_rest_ring.setText(restSound);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.isPressed()) {
            if (buttonView.getId() == R.id.switch_auto_rest) {
                autoRest = !autoRest;
                editor.putBoolean("autoRest", autoRest);
                editor.apply();
            }
            if (buttonView.getId() == R.id.switch_vibration) {
                vibration = !vibration;
                editor.putBoolean("vibration", vibration);
                editor.apply();
            }
        }
    }
}